package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.entity.OtherItem;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class OtherAdapter extends RecyclerView.Adapter<OtherAdapter.OtherViewHolder> {
    private List<OtherItem.ArticlesEntity> entities;
    private ImageLoader imageLoader;
    private Context context;

    public OtherAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<OtherItem.ArticlesEntity> datas) {
        entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public OtherViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_cartoon_message_item, null);
        return new OtherViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OtherViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            OtherItem.ArticlesEntity entity = entities.get(position);
            if (entity.getCovers().size() > 0){
                String url = entity.getCovers().get(0).getU();
                holder.imageView.setImageUrl(url,imageLoader);
                holder.textView.setText(entity.getTitle());
                holder.textViewP.setText(entity.getComment_count()+"");
                holder.textViewZ.setText(entity.getUp_count()+"");

            }
        }
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class OtherViewHolder extends RecyclerView.ViewHolder {
        private TextView textView, textViewZ, textViewP;
        private NetworkImageView imageView;

        public OtherViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.cartoonMessageItemTitle);
            imageView = (NetworkImageView) itemView.findViewById(R.id.cartoonMessageItemImg);
            textViewZ = (TextView) itemView.findViewById(R.id.TextViewForZan);
            textViewP = (TextView) itemView.findViewById(R.id.TextViewForPL);
        }
    }
}
